package com.cqq.pro.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenjun
 * 日期处理util
 */
public final class DateUtil {
	private final static String DATEMONTHFORMAT = "yyyy-MM";
	private final static String DATEFORMAT = "yyyy-MM-dd";
	private final static String DATEMIFORMAT = "yyyy-MM-dd HH:mm";
	private final static String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
	private final static DateFormat dateMiFormat = new SimpleDateFormat(DATEMIFORMAT);
	private final static DateFormat dateMonthFormat = new SimpleDateFormat(DATEMONTHFORMAT);
	private final static DateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
	private final static DateFormat dateTimeFormat = new SimpleDateFormat(DATETIMEFORMAT);
	
	// 格式化日期
	public static String formatDate(Date date) {
		return dateFormat.format(date);
	}
	
	// 格式化日期
	public static String formatDate(Date date,String formate) {
		return new SimpleDateFormat(formate).format(date);
	}
    
    public static String formateDateMonth(Date date) {
		return dateMonthFormat.format(date);
	}
	
	// 格式化日期
	public static String formatDate(long date) {
		return dateFormat.format(new Date(date));
	}
	
	// 格式化日期时间 yyyy-MM-dd hh:mm
	public static final String formatDateTimeMi(Date date) {
		return dateMiFormat.format(date);
	}
	// 格式化日期时间
	public static final String formatDateTime(Date date) {
		return dateTimeFormat.format(date);
	}
	
	// 格式化日期
	public static final String formateDateTime(long date) {
		return dateTimeFormat.format(new Date(date));
	}
        
        //返回当前时间的前一个小时时间
        public static final String getCurrDateBeforeOneHour(){
            Calendar   ca   =   Calendar.getInstance();
            ca.set(Calendar.HOUR_OF_DAY,   ca.get(Calendar.HOUR_OF_DAY)-1);
            Date   firstDate   =   ca.getTime();
            return dateTimeFormat.format(firstDate);
        }
	
	// 获取当前的timestamp
	public static final Timestamp getCurrentTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}
        //获得某个日期的上半年，或者下半年的开始月份
        public static String getHalfYearOfDateStr(String dateStr) {
            Date dateMonth = new Date();
             try {
                 dateMonth = dateFormat.parse(dateStr);
             } catch (ParseException ex) {
             }
             Calendar   ca   =   Calendar.getInstance();
             ca.setTime(dateMonth);
             int month = ca.get(Calendar.MONTH);
             if(month>=6){
                 ca.add(Calendar.MONTH, 6-month);
             }else{
                 ca.add(Calendar.MONTH, -month);
             }
        return dateMonthFormat.format(ca.getTime());
    }
        
        //获得日期对应的星期的第一天
        public static String getFirstDateOfWeekByDateStr(String dateStr) {
            Date dateMonth = new Date();
             try {
                 dateMonth = dateFormat.parse(dateStr);
             } catch (ParseException ex) {
             }
             Calendar   ca   =   Calendar.getInstance();
             ca.setTime(dateMonth);
             int day = ca.get(Calendar.DAY_OF_WEEK);
             Map daysOfWeek = new HashMap();
             daysOfWeek.put(2, 0);
             daysOfWeek.put(3, 1);
             daysOfWeek.put(4, 2);
             daysOfWeek.put(5, 3);
             daysOfWeek.put(6, 4);
             daysOfWeek.put(7, 5);
             daysOfWeek.put(1, 6);
             int count = Integer.parseInt(daysOfWeek.get(day).toString());
             ca.add(Calendar.DAY_OF_MONTH,-count);
        return dateFormat.format(ca.getTime());
    }
	
        public static String getWeekOfDateStr(String dateStr) {
       Date dateMonth = new Date();
        try {
            dateMonth = dateFormat.parse(dateStr);
        } catch (ParseException ex) {
        }
        Calendar   ca   =   Calendar.getInstance();
        ca.setTime(dateMonth);
        int day = ca.get(Calendar.DAY_OF_WEEK);
        Map daysOfWeek = new HashMap();
         daysOfWeek.put(2, "周一");
         daysOfWeek.put(3, "周二");
         daysOfWeek.put(4, "周三");
         daysOfWeek.put(5, "周四");
         daysOfWeek.put(6, "周五");
         daysOfWeek.put(7, "<font color=\"#FF0000\">周六</font>");
         daysOfWeek.put(1, "<font color=\"#FF0000\">周日</font>");
        return daysOfWeek.get(day).toString();
    }
        
	public static final Timestamp getTimestamp(long date) {
		return new Timestamp(date);
	}
        
        /**
         * 格式化Timestamp日期 Timestamp-->字符串
         * sharefun2068
         * 2013-12-17
         * @param date
         * @return 
         */
	public static  String formateTimestamp2Str(Timestamp date) {
              if(date!=null){
                  return dateTimeFormat.format(date);
             } else{
                  return "";
              }
	}      
        /**
         * 格式化Timestamp日期 Timestamp-->字符串
         * chenqq
         * 2014-07-04
         * @param date
         * @return yyyy-mm-dd
         */
	public static  String formateTimestamp2(Timestamp date) {
              if(date!=null){
                  return dateFormat.format(date);
             } else{
                  return "";
              }
	}  
        
        /**获取当前日期，加一天
         * chenqq
         * 2014-07-04
         * @return 
         */
        public static Date getCurrentDateAfterAddOne(){
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, 1);
            return c.getTime();
        }
        
        /**获取当前日期，加一天
         * chenqq
         * 2014-07-04
         * @return 
         */
        public static Date getDateAfterAddOne(String day){
            Date  date = new Date();
            try {
                date = dateFormat.parse(day);
            } catch (ParseException ex) {
            }
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            return c.getTime();
        }
        
        /**
         * 获得当前月的第一天
         * @return 
         */
        public static Date getFirstDateOfCurrentMonth(){
            Calendar   ca   =   Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH,   1);
            return ca.getTime();
        }
        
         /**
         * 获得某个月的第一天
         * @return 
         */
        public static Date getFirstDateOfMonth(String month){
            Date dateMonth = new Date();
            try {
                dateMonth = dateMonthFormat.parse(month);
            } catch (ParseException ex) {
            }
            Calendar   ca   =   Calendar.getInstance();
            ca.setTime(dateMonth);
            ca.set(Calendar.DAY_OF_MONTH,   1);
            return ca.getTime();
        }
        
        public static Date getLastDateOfMonth(String month){
            Date dateMonth = new Date();
            try {
                dateMonth = dateMonthFormat.parse(month);
            } catch (ParseException ex) {
            }
            Calendar   ca   =   Calendar.getInstance();
            ca.setTime(dateMonth);
            ca.add(Calendar.MONTH, 1);
            ca.set(Calendar.DAY_OF_MONTH,   0);
            return ca.getTime();
        }
        
        public static Date getLastDateOfCurrMonth(){
            Date dateMonth = new Date();
            Calendar   ca   =   Calendar.getInstance();
            ca.setTime(dateMonth);
            ca.add(Calendar.MONTH, 1);
            ca.set(Calendar.DAY_OF_MONTH,   0);
            return ca.getTime();
        }
        /**
         * 格式化Timestamp日期 字符串-->Timestamp 
         * String的类型必须形如： yyyy-mm-dd hh:mm:ss[.f...] 这样的格式，中括号表示可选，否则报错！！！ 
         * sharefun2068
         * 2013-12-17
         * @param date
         * @return 
         */
	public static  Timestamp formateStr2Timestamp(String date) {
              Timestamp ts = new Timestamp(System.currentTimeMillis());
		try {
			ts = Timestamp.valueOf(date);
		} catch (Exception e) {
		}
               return ts;
	} 
        
        /**
         * @param dateStr yyyy-mm-dd
         * @return date
         */
      public static  Date formateStrToTDate(String dateStr) {
        Date ts = new Date();
        try {
            ts = dateFormat.parse(dateStr);
        } catch (ParseException ex) {
        }
        return ts;
      } 
        /**
         * 格式化Timestamp日期
         * 如 11月12日
         * @param date
         * @return 
         */
  	public static  String formateTimestamp2MMDDHHMM(Timestamp tmpTimestamp) {
                   // int year = tmpTimestamp.getYear()+1900;
              if(tmpTimestamp!=null){
                    int month = tmpTimestamp.getMonth() + 1;
                    int day = tmpTimestamp.getDate();
                    int hour = tmpTimestamp.getHours();
                    int minute=tmpTimestamp.getMinutes();
                    return month+"月"+day+"日 "+((hour)<10?(0+""+hour):hour)+":"+((minute)<10?(0+""+minute):minute);
              }else{
                  return "";
              }
	}      
  	
  	public static  String formateTimestamp2MMDD(Timestamp tmpTimestamp) {
        // int year = tmpTimestamp.getYear()+1900;
	   if(tmpTimestamp!=null){
	         int month = tmpTimestamp.getMonth() + 1;
	         int day = tmpTimestamp.getDate();
	         return month+"月"+day+"日 ";
	   }else{
	       return "";
	   }
	}
  	
  	/**
  	 * 字符串的日期格式的计算 
  	 * @param smdate
  	 * @param bdate
  	 * @return
  	 * @throws ParseException
  	 */
  	public static int daysBetween(String smdate,String bdate) throws ParseException{  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(dateFormat.parse(smdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(dateFormat.parse(bdate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));     
    }  
  	
  	/**获取当前日期，加一天
     * chenqq
     * 2014-07-04
     * @return 
     */
    public static Date getDateAfterAddDays(String dateStr,int days){
        Date  date = new Date();
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException ex) {
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, days);
        return c.getTime();
    }
    /**获取当前日期，加一天
     * chenqq
     * 2014-07-04
     * @return 
     */
    public static Timestamp getTimestampAfterAddDays(String dateStr,int days){
    	Date  date = new Date();
    	try {
    		date = dateFormat.parse(dateStr);
    	} catch (ParseException ex) {
    	}
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.add(Calendar.DAY_OF_MONTH, days);
    	return DateUtil.getTimestamp(c.getTime().getTime());
    }
    
    public static String getWeekOfDateStr(Timestamp time) {
    	if(time==null){
    		time = new Timestamp(System.currentTimeMillis());
    	}
         Calendar   ca   =   Calendar.getInstance();
         ca.setTime(time);
         int day = ca.get(Calendar.DAY_OF_WEEK);
         Map daysOfWeek = new HashMap();
         daysOfWeek.put(2, "周一");
         daysOfWeek.put(3, "周二");
         daysOfWeek.put(4, "周三");
         daysOfWeek.put(5, "周四");
         daysOfWeek.put(6, "周五");
         daysOfWeek.put(7, "<font color=\"#FF0000\">周六</font>");
         daysOfWeek.put(1, "<font color=\"#FF0000\">周日</font>");
         return daysOfWeek.get(day).toString();
     }
    
    /**
     * 为某字符串日期：如2015-01-09  加，减天数 获得新的日期
     * @param dateStr
     * @param days
     * @return 
     */
    public static String  addDaysForDate(String dateStr,int days){
        Date date =new Date(dateStr.replaceAll("-", "/"));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //加天
        cal.add(Calendar.DATE, days);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
    }
    
  //返回当前时间加days天后的时间 yyyy-MM-dd
    public static final String getCurrDateAfterAddDays(Integer days){
        Calendar   ca   =   Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH,   ca.get(Calendar.DAY_OF_MONTH)+days);
        Date   firstDate   =   ca.getTime();
        return dateFormat.format(firstDate);
    }
    
    public static Date getStartTime(Date date){
		if(date == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	public static Date getEndTime(Date date){
		if(date == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	public static Date getSkipDate(Date date, int num){
		if(date == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, num);
		return calendar.getTime();
	}
    
}
